package org.flower.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.flower.bean.User;
import org.flower.bean.UserExample;
import org.flower.dao.UserDao;
import org.flower.dao.impl.UserDaoImpl;
import org.flower.mapper.UserMapper;
import org.flower.service.UserService;
import org.flower.utils.MyBatisUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Aizen
 */
public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findAll(org.flower.bean.User user, int start){
        SqlSession sqlSessionFactory = null;
        try {
            sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        }
        UserMapper mapper = sqlSessionFactory.getMapper(UserMapper.class);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        //条件查询
        if(user.getName()!=null){
            criteria.andNameLike("%"+user.getName()+"%");
        }
        if(user.getPhone()!=null){
            criteria.andPhoneLike("%"+user.getPhone()+"%");
        }
        if(user.getSex()!=null&&!user.getSex().equals("")){
            criteria.andSexEqualTo(user.getSex());
        }

        if(user.isFlag()){
            criteria.andStateEqualTo(user.getState());
        }

        if(user.getLogindate()!=null){
            Date logindate = user.getLogindate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(logindate);
            criteria.andLoginDateLike("%"+format+"%");
        }

        PageHelper.startPage(start,10);
        List<User> users = mapper.selectByExample(userExample);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (User u:users) {
            if(u.getLogindate()!=null&&!u.getLogindate().equals("")){
                String logindate = sdf.format(u.getLogindate());
                u.setLogindatestr(logindate);
            }
            if(u.getBorndate()!=null&&!u.getBorndate().equals("")){
                String birthday = sdf.format(u.getBorndate());
                u.setBirthday(birthday);
            }
        }

        PageInfo<User> userPageInfo = new PageInfo<User>(users,10);
        sqlSessionFactory.close();
        return userPageInfo;

    }

    @Override
    public org.flower.entity.User getByid(int id) {
        UserDao userDao = new UserDaoImpl();
        return  userDao.getByid(id);
    }

    @Override
    public boolean UpdateById(org.flower.entity.User user) {
        UserDao userDao = new UserDaoImpl();
        return userDao.UpdateById(user);
    }

    @Override
    public boolean UpdatePwd(org.flower.entity.User user) {
        UserDao userDao = new UserDaoImpl();
        return userDao.UpdatePwd(user);
    }

}
